package com.ll.gulimall.ware.controller;

import java.math.BigDecimal;
import java.util.*;

import com.ll.common.exception.BizCodeEnume;
import com.ll.common.to.SkuHasStockTo;
import com.ll.gulimall.ware.vo.FareAndAddressVo;
import com.ll.gulimall.ware.vo.WareLockResponseVo;
import com.ll.gulimall.ware.vo.WareLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ll.gulimall.ware.entity.WareSkuEntity;
import com.ll.gulimall.ware.service.WareSkuService;
import com.ll.common.utils.PageUtils;
import com.ll.common.utils.R;


/**
 * 商品库存
 *
 * @author liliang
 * @email liliang@gmail.com
 * @date 2020-07-31 01:02:00
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    @GetMapping("/freight")
    public R getFreight(@RequestParam("addrId") Long addrId) {
        FareAndAddressVo address = wareSkuService.freight(addrId);
        return R.ok().setData(address);
    }


    /**
     * 锁定库存
     *
     * @param wareLockVo 需要锁定的数据
     * @return
     */
    @PostMapping("/sku/lock")
    public R skuLock(@RequestBody WareLockVo wareLockVo) {
        try {
            Boolean result = wareSkuService.skuLock(wareLockVo);
            //锁定成功
            return R.ok();
        } catch (RuntimeException e) {
            //锁定失败
            return R.error(BizCodeEnume.WARE_LOCK_EXCEPTION.getCode(), BizCodeEnume.WARE_LOCK_EXCEPTION.getMsg());
        }

    }




    /**
     * 根据SkuIds 查询该商品是否有库存
     *
     * @param skuIds
     * @return
     */
    @PostMapping("/hasStock")
    public R hasStock(@RequestBody List<Long> skuIds) {
        List<SkuHasStockTo> skuHasStockTos = wareSkuService.hasStock(skuIds);
        R r = R.ok().setData(skuHasStockTos);
        return r;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
